package cn.hengzhu.main_manager.web.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.hengzhu.main_manager.repository.dao.ManageCaseMapper;
import cn.hengzhu.main_manager.repository.dao.ManageCompMapper;
import cn.hengzhu.main_manager.repository.domain.ManageCase;
import cn.hengzhu.main_manager.repository.domain.ManageCaseExample;
import cn.hengzhu.main_manager.repository.domain.ManageComp;
import cn.hengzhu.main_manager.repository.domain.ManageCompExample;
import cn.hengzhu.main_manager.repository.param.PageBaseParam;
import cn.hengzhu.main_manager.repository.vo.CompListVO;
import cn.hengzhu.main_manager.service.ManageCaseBoxService;
import cn.hengzhu.main_manager.utils.AjaxResult;
import cn.hengzhu.main_manager.utils.DateTimeHelper;
import cn.hengzhu.main_manager.utils.LayUiResultJson;
import cn.hengzhu.main_manager.utils.ann.MoblieUrl;
import cn.hengzhu.main_manager.utils.ann.SystemControllerLog;
@Controller
@RequestMapping("/comp")
public class CompController {

	@Autowired
	private ManageCompMapper manageCompMapper;

	@Autowired
	private ManageCaseBoxService manageCaseBoxService;
	@Autowired
	private HttpServletRequest request;
	
	@Autowired
	private ManageCaseMapper manageCaseMapper;

	@MoblieUrl
	@RequestMapping("/index")
	public String getComp() {
		return request.getAttribute("moblie")+"comp";
	}
	
	@MoblieUrl
	@RequestMapping("/editOrAdd/index")
	public String editOrAddComp() {
		return request.getAttribute("moblie")+"compEdit";
	}

	@ResponseBody
	@RequestMapping("/getCompList")
	public LayUiResultJson<CompListVO> getCompList(PageBaseParam param) {

		PageHelper.startPage(param.getPage(), param.getLimit(), true);
		ManageCompExample example = new ManageCompExample();
		example.setOrderByClause("CREATE_TIME desc");
		List<ManageComp> compList = manageCompMapper.selectByExample(example);
		PageInfo<ManageComp> pageInfo = new PageInfo<>(compList);
		long count = pageInfo.getTotal();

		List<CompListVO> list = new ArrayList<>();

		compList.stream().forEach(a -> {
			CompListVO compListVO = new CompListVO();
			compListVO.setCompId(a.getCompId());
			compListVO.setCompName(a.getCompName());
			compListVO.setDes(a.getDes());
			compListVO.setManagerName(a.getManagerName());
			compListVO.setMobilePhone(a.getMobilePhone());
			compListVO.setCreatTime(DateTimeHelper.formatDateTimetoString(a.getCreateTime(), "yyyy-MM-dd HH:mm"));
			list.add(compListVO);
		});
		LayUiResultJson<CompListVO> result = new LayUiResultJson<>(count, list);
		return result;
	}

	@ResponseBody
	@RequestMapping("/editOrAddComp")
	@SystemControllerLog(description = "修改或添加公司信息")
	public AjaxResult editOrAddComp(ManageComp param) {
		Integer compId = param.getCompId();
		ManageCompExample mce = new ManageCompExample();
		mce.createCriteria().andCompNameEqualTo(param.getCompName());
		List<ManageComp> selectByExample = manageCompMapper.selectByExample(mce);
		if (selectByExample.isEmpty()) {
			if (compId != null) {// 修改
				try {
					manageCompMapper.updateByPrimaryKeySelective(param);
					return new AjaxResult();
				} catch (Exception e) {
					return new AjaxResult("修改失败");
				}
			} else {
				try {
					param.setCreateTime(new Date());
					manageCaseBoxService.addComp(param);
					return new AjaxResult();
				} catch (Exception e) {
					return new AjaxResult("添加失败");
				}
			}
		} else if (selectByExample.size() == 1) {
			String compName = selectByExample.get(0).getCompName();
		/*	if(compName.equals(param.getCompName())){
				if(compId == null) {
					return new AjaxResult("已有此公司");
				}else if(compId.equals(selectByExample.get(0).getCompId())) {
					try {
						manageCompMapper.updateByPrimaryKeySelective(param);
						return new AjaxResult();
					} catch (Exception e) {
						return new AjaxResult("修改失败");
					}*/
			if (compName.equals(param.getCompName()) && compId.equals(selectByExample.get(0).getCompId())) {
				try {
					manageCompMapper.updateByPrimaryKeySelective(param);
					return new AjaxResult();
				} catch (Exception e) {
					return new AjaxResult("修改失败");
				}
			}
		}
		return new AjaxResult("已有此公司或填写错误错误");
	}

	@ResponseBody
	@RequestMapping("/deleteComp")
	@SystemControllerLog(description = "删除公司")
	public AjaxResult deleteComp(Integer compId) {
		// 查询是否有储物柜

		ManageCaseExample manageCaseExample = new ManageCaseExample();
		manageCaseExample.createCriteria().andCompIdEqualTo(compId);
		List<ManageCase> manageCaseList = manageCaseMapper.selectByExample(manageCaseExample);
		if (!manageCaseList.isEmpty()) {
			return new AjaxResult("该公司还有储物柜，不能删除");
		} else {
			try {
				manageCompMapper.deleteByPrimaryKey(compId);
				return new AjaxResult();
			} catch (Exception e) {
				return new AjaxResult("删除失败");
			}

		}

	}

}
